﻿using ByteSite.WinClient.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;
using Websocket.Client;

namespace ByteSite.WinClient.Common
{
    public static class WebSocketHelper
    {
        public static string sKey = "ByTeSiTe"; // 密钥 消息AES/DES加密
        public static string Separator = "◥◣BS◢◤"; // 套接字分隔符
        public static TransferSocket tSocket = new TransferSocket(); // 套接字
        public static TransferFilesSeting tRules = new TransferFilesSeting(); // 文件传输规则

        /// <summary>
        /// 文件拆分包
        /// </summary>
        /// <param name="packFlag">包标识</param>
        /// <param name="fileName">文件名</param>
        /// <param name="packSize">包大小 kb</param>
        /// <returns></returns>
        public static void SendFileBySplit(this WebsocketClient websocket, string guid,string clientId, string packFlag, byte[] data, int packSize)
        {
            string Zipstr = Utils.ZipString(Convert.ToBase64String(data)); // 压缩字符串

            int packNum = (int)Math.Ceiling(Zipstr.Length / (double)packSize); // 包总数
            string pack = null;
            
            // 分包线程
            Thread progress = new Thread(() =>
            {
                for (int i = 0; i < packNum - 1; i++)
                {
                    pack = Zipstr.Substring(i * packSize, packSize);
                    websocket.Send(

                        JsonHelper.ObjectToJSON(new WebSocketMessage()
                        {
                            Guid = guid,
                            From = "client_" + clientId,
                            To = "server",
                            Time = DateTime.Now,
                            Channel = "client",
                            MessageType = "sys",
                            Message = "remote_desktop_packet",
                            Data = pack,
                            DataNo = i,
                            DataCount = packNum
                        })
                    );
                }
                // 最后剩余字节打一个包
                pack = Zipstr.Substring((packNum - 1) * packSize, Zipstr.Length - (packNum - 1) * packSize);
                // 向服务器发送包：套接字、包标识、包总数、包序号、包内容
                websocket.Send(
                    JsonHelper.ObjectToJSON(new Entitys.WebSocketMessage()
                    {
                        Guid = guid,
                        From = "client_" + clientId,
                        To = "server",
                        Time = DateTime.Now,
                        Channel = "client",
                        MessageType = "sys",
                        Message = "remote_desktop_packet",
                        Data = pack,
                        DataNo = packNum - 1,
                        DataCount = packNum
                    })
                );
                websocket.Send(
                    JsonHelper.ObjectToJSON(new Entitys.WebSocketMessage()
                    {

                        Guid = guid,
                        From = "client_" + clientId,
                        To = "server",
                        Time = DateTime.Now,
                        Channel = "client",
                        MessageType = "sys",
                        Message = "remote_desktop_packet_over",
                        Data = "",
                        DataNo = 0,
                        DataCount = packNum
                    })
                );
            });
            //开始线程
            progress.Start();
        }
    }
}
